<template>
  <div>
    <!-- 顶部导航 -->
    <van-sticky :offset-top="0">
      <van-nav-bar
        title="商品搜索"
        left-text="返回"
        left-arrow
        @click-left="onClickLeft"
      />
    </van-sticky>
    <!-- 搜索栏 -->
    <form action="/">
      <van-search
        v-model="value"
        show-action
        placeholder="请输入搜索关键词"
        @input="onInput"
        @search="onSearch"
        @cancel="onCancel"
      />
    </form>

    <div class="historyhot" v-if="act == ''">
      <!-- 历史记录 -->
      <div class="history">
        <div class="top">
          <h3 class="title">历史记录</h3>
          <van-icon @click="delhistory" name="delete" />
        </div>
        <div class="con">
          <span
            @click="togoods(item.keyword)"
            v-for="item in historyData"
            :key="item.id"
            >{{ item.keyword }}</span
          >
        </div>
      </div>
      <!-- 热门搜索 -->
      <div class="history hot">
        <div class="top">
          <h3 class="title">热门搜索</h3>
        </div>
        <div class="con">
          <span
            :class="index == 0 ? 'act' : ''"
            v-for="(item, index) in hotKeywordList"
            :key="item.id"
            @click="togoods(item.keyword)"
            >{{ item.keyword }}</span
          >
        </div>
      </div>
    </div>

    <!-- 搜索出来的内容 -->
    <ul class="search" v-else-if="act == 'search'">
      <li
        v-for="item in searchlist"
        @click="togoods(item.name)"
        :class="!searchlist.length == 1 ? 'active' : ''"
        :key="item.id"
      >
        {{ item.name }}
      </li>
    </ul>

    <!-- 点击进入商品 -->
    <div class="goods" v-else>
      <div class="sort">
        <div
          class="item"
          @click="change('综合')"
          :class="num == 1 ? 'act' : ''"
        >
          综合
        </div>
        <div
          class="item"
          @click="change('价格')"
          :class="num == 2 ? 'act' : ''"
        >
          价格
        </div>
        <div
          class="item"
          @click="change('分类')"
          :class="num == 3 ? 'act' : ''"
        >
          分类
        </div>
      </div>
      <!-- 商品列表 -->
      <div class="goods">
        <div
          class="item"
          @click="togoodsinfo(item.id)"
          v-for="item in datalist"
          :key="item.id"
        >
          <img :src="item.list_pic_url" alt="" />
          <h3>{{ item.name }}</h3>
          <p>{{ item.retail_price }}</p>
        </div>
      </div>
      <li class="isshow" v-show="show">数据库内暂无相关商品</li>
    </div>
  </div>
</template>

<script>
import {
  index,
  goodsList,
  detailaction,
  listaction,
  goodsdetailaction,
  addcollect,
  // 搜索
  helperaction,
  indexaction,
  addhistoryaction,
  clearhistoryAction,
} from "@/api/home/index";

export default {
  components: {},

  data() {
    return {
      show: false,
      value: "",
      historyData: [],
      hotKeywordList: [],
      datalist: [],
      searchlist: [],
      num: 1,
      count: 1,
      act: "", // "" 和 search  和  goods
    };
  },

  created() {
    indexaction({
      openId: localStorage.getItem("openId"),
    }).then((res) => {
      console.log(res);
      this.historyData = res.historyData;
      this.hotKeywordList = res.hotKeywordList;
    });
  },

  mounted() {},

  methods: {
    //到商品详细页
    togoodsinfo(goodsid) {
      console.log(goodsid);
      this.$router.push({
        name: "goodsinfo",
        params: {
          id: goodsid,
        },
      });
    },
    //   点击搜索出来的内容跳转
    togoods(name) {
      this.act = "goods";
      console.log(name);
      helperaction({
        keyword: name,
      }).then((res) => {
        console.log(res);
        this.datalist = res.keywords;
        if (res.keywords.length == 0) {
          this.show = true;
        } else {
          this.show = false;
        }
      });
    },
    //   删除历史记录
    delhistory() {
      clearhistoryAction({
        openId: localStorage.getItem("openId"),
      }).then((res) => {
        console.log(res);
      });
      this.$router.go(0);
    },
    onClickLeft() {
      this.$router.go(-1);
    },
    // 输入就触发
    onInput(val) {
      this.act = "search";
      if (this.value == "") {
        this.act = "";
      }
      helperaction({
        keyword: this.value,
        order: "",
      }).then((res) => {
        if (res.keywords.length == 0) {
          this.searchlist = [{ name: "数据库内暂无相关数据!!" }];
        } else {
          this.searchlist = res.keywords;
        }
        console.log(res);
      });
    },
    onSearch(val) {
      addhistoryaction({
        keyword: val,
        openId: localStorage.getItem("openId"),
      }).then((res) => {
        console.log(res);
      });
      this.value = "";
    },
    onCancel() {},
    //   切换分类
    change(type) {
      if (type == "综合") {
        this.num = 1;
      } else if (type == "价格") {
        this.num = 2;
      } else {
        this.num = 3;
      }
    },
  },

  computed: {},

  watch: {},
};
</script>

<style scoped lang='less'>
// 历史记录
.history {
  background-color: #fff;
  width: 375px;
  margin-top: 10px;
  i {
    line-height: 22px;
  }
  .top {
    width: 335px;
    height: 20px;
    padding: 10px 20px;
    display: flex;
    justify-content: space-between;
  }
  .con {
    width: 335px;
    padding: 10px 20px 30px 20px;
    overflow: hidden;
    span {
      float: left;
      padding: 5px;
      border: 1px solid rgb(161, 161, 161);
      margin-top: 10px;
      margin-right: 10px;
    }
    .act {
      border: 1px solid red;
      color: red;
    }
  }
}

// 搜索出来的东西
.search {
  width: 375px;
  background-color: #fff;
  li {
    width: 335px;
    padding: 10px 20px;
    text-align: left;
  }
  .active {
    height: 300px;
    width: 335px;
    line-height: 300px;
    text-align: center;
    color: red;
    padding: 10px 20px;
  }
}
.isshow{
  width: 375px;
  list-style: none;
  text-align: center;
  padding: 30px 0;
}
.goods {
  width: 375px;
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  .item {
    width: 185px;
    margin-top: 5px;
    background-color: #fff;
    img {
      width: 151px;
    }
    h3 {
      margin: 5px auto;
    }
    p {
      color: red;
      margin-bottom: 20px;
    }
  }
}
.banner {
  width: 375px;
}
.sort {
  margin-top: -2px;
  width: 375px;
  background-color: #fff;
  display: flex;
  justify-content: space-around;
  .item {
    width: 54px;
    height: 40px;
    text-align: center;
    line-height: 40px;
  }
  .icon {
    height: 34px;
    width: 10px;
    padding-top: 6px;
  }
  .act {
    color: red;
  }
}
</style>